package 回溯;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TTDB
 * @create 2025/9/18
 */
public class letterCombinations {
     private static final String[] MAPPING = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
     public List<String> letterCombinations(String digits) {
          int n = digits.length();
          if(n == 0) return new ArrayList<>();
          // 初始化结果
          List<String> res = new ArrayList<>();
          //开始dfs遍历
          dfs(res, "", digits, 0);
          return res;
     }
     public void dfs(List<String> res, String path, String digits, int start) {
          //终止条件
          if(start == digits.length()) {
               res.add(path);
               return;
          }
          //当前值
          String letters = MAPPING[digits.charAt(start) - '0'];//字符-字符可以得到数字
          //创建多个分支，遍历
          for(int i = 0; i < letters.length(); i++) {
               dfs(res, path + letters.charAt(i), digits, start + 1);
          }
     }
}
